<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\AdminUser\Service;

use Hyperf\Database\Model\Builder;
use MoChat\Framework\Service\AbstractService;
use MoChat\Plugin\AdminUser\Contract\AdminUserContract;
use MoChat\Plugin\AdminUser\Model\AdminUser;

class AdminUserService extends AbstractService implements AdminUserContract
{
    /**
     * @var AdminUser
     */
    protected $model;

    /**
     * 查询单条 - 根据ID.
     * @param int $id ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getAdminUserById(int $id, array $columns = ['*']): array
    {
        return $this->model->getOneById($id, $columns);
    }

    /**
     * 查询多条 - 根据ID.
     * @param array $ids ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getAdminUsersById(array $ids, array $columns = ['*']): array
    {
        return $this->model->getAllById($ids, $columns);
    }

    /**
     * 多条分页.
     * @param array $where 查询条件
     * @param array|string[] $columns 查询字段
     * @param array $options 可选项 ['orderByRaw'=> 'id asc', 'perPage' => 15, 'page' => null, 'pageName' => 'page']
     * @return array 分页结果 Hyperf\Paginator\Paginator::toArray
     */
    public function getAdminUserList(array $where, array $columns = ['*'], array $options = []): array
    {
        return $this->model->getPageList($where, $columns, $options);
    }

    /**
     * 添加单条
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createAdminUser(array $data): int
    {
        return $this->model->createOne($data);
    }

    /**
     * 添加多条
     * @param array $data 添加的数据
     * @return bool 执行结果
     */
    public function createAdminUsers(array $data): bool
    {
        return $this->model->createAll($data);
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $id id
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateAdminUserById(int $id, array $data): int
    {
        return $this->model->updateOneById($id, $data);
    }

    /**
     * 删除 - 单条
     * @param int $id 删除ID
     * @return int 删除条数
     */
    public function deleteAdminUser(int $id): int
    {
        return $this->model->deleteOne($id);
    }

    /**
     * 删除 - 多条
     * @param array $ids 删除ID
     * @return int 删除条数
     */
    public function deleteAdminUsers(array $ids): int
    {
        return $this->model->deleteAll($ids);
    }

    /**
     * 查询单条 - 根据手机号.
     * @param string $phone 手机号
     * @param array $columns 查询字段
     * @return null|AdminUser|Builder|object
     */
    public function getAdminUserByPhone(string $phone, array $columns = ['*'])
    {
        return $this->model::query()->where('phone', $phone)->first($columns);
    }

    /**
     * {@inheritdoc}
     */
    public function getAdminUserModelById(int $id, array $columns = ['*'])
    {
        return $this->model::query()->where('id', $id)->first($columns);
    }

    /**
     * 修改账户状态 - 根据ID.
     * @param array $ids id
     * @param int $status 账户状态
     * @return int 修改条数
     */
    public function updateAdminUserStatusById(array $ids, int $status): int
    {
        return $this->model::query()->whereIn('id', $ids)->update(['status' => $status]);
    }

    /**
     * 修改账户密码 - 根据ID.
     * @param int $id id
     * @param string $password 密码
     * @return int 修改条数
     */
    public function updateAdminUserPasswordById(int $id, string $password): int
    {
        return $this->model::query()->where('id', $id)->update(['password' => $password]);
    }

    /**
     * 查询多条 - 根据手机号.
     * @param array $phones 手机号
     * @param array $columns 查询字段
     * @return array 响应数组
     */
    public function getAdminUsersByPhone(array $phones, array $columns = ['*']): array
    {
        return $this->model::query()->whereIn('phone', $phones)->get($columns)->toArray();
    }

    /**
     * 查询多条.
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getAdminUsers(array $columns = ['*']): array
    {
        return $this->model::query()->get($columns)->toArray();
    }

    /**
     * 查询多条 - 根据代理级别ID.
     * @param array $columns 查询字段
     * @return array 响应数组
     */
    public function getAdminUsersByLevel(int $level, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('level', $level)
            ->where('status', 1)
            ->get($columns);

        $res || $res = collect([]);

        return $res->toArray();
    }

    /**
     * 修改账户状态 - 根据手机号.
     * @param int $status 账户状态
     * @return int 修改条数
     */
    public function updateAdminUserStatusByPhone(string $phone, int $status): int
    {
        return $this->model::query()
            ->where('phone', $phone)
            ->update(['status' => $status]);
    }

    /**
     * @param array|string[] $columns
     */
    public function getAdminUserListBySearch(int $parentId, int $status, string $phone, string $startTime, string $endTime, int $level, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('is_root', 0)
            ->when($parentId > 0, function (Builder $query) use ($parentId) {
                return $query->where('parent_id', $parentId);
            })
            ->when($status > 0, function (Builder $query) use ($status) {
                return $query->where('status', $status);
            })
            ->when(! empty($phone), function (Builder $query) use ($phone) {
                return $query->where('name', 'like', "%{$phone}%")
                    ->orWhere('phone', 'like', "%{$phone}%");
            })
            ->when(! empty($startTime), function (Builder $query) use ($startTime, $endTime) {
                return $query->whereBetween('created_at', [$startTime, $endTime]);
            })
            ->when($level > 0, function (Builder $query) use ($level) {
                return $query->where('level', $level);
            })
            ->orderByDesc('created_at')
            ->get($columns);
        $res || $res = collect([]);

        return $res->toArray();
    }

    /**
     * 查询多条 - 根据代理级别ID.
     * @param array $columns 查询字段
     * @return array 响应数组
     */
    public function getAdminUserByStatus(int $id, int $status, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('id', $id)
            ->where('status', $status)
            ->first($columns);
        $res || $res = collect([]);

        return $res->toArray();
    }

    /**
     * 软删除查询单条 - 根据ID.
     * @param int $id ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getAdminUserByIdWithTrashed(int $id, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('id', $id)
            ->withTrashed()
            ->first($columns);
        $res || $res = collect([]);
        return $res->toArray();
    }

    /**
     * @param array|string[] $columns
     */
    public function getAdminUserByParentId(int $parentId, int $status, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('parent_id', $parentId)
            ->where('status', $status)
            ->get($columns);
        $res || $res = collect([]);
        return $res->toArray();
    }

    public function countAdminUserByParentId(int $parentId, int $status, string $startTime, string $endTime): int
    {
        return $this->model::query()
            ->where('parent_id', $parentId)
            ->where('status', $status)
            ->when(! empty($startTime), function (Builder $query) use ($startTime, $endTime) {
                return $query->whereBetween('created_at', [$startTime, $endTime]);
            })
            ->count('id');
    }

    /**
     * 查询单条 - 根据是否为超级管理员.
     * @param int $isRoot 是否为超级管理员 1-是 0-不是
     * @param array $columns 查询字段
     * @return null|AdminUser|Builder|object
     */
    public function getAdminUserByIsRoot(int $isRoot, array $columns = ['*']): array
    {
        $res = $this->model::query()->where('is_root', $isRoot)->first($columns);
        $res || $res = collect([]);
        return $res->toArray();
    }
}
